﻿''' <summary>
''' COPY句管理窗体
''' </summary>
''' <remarks></remarks>
Public Class frmCopyBook
    ''' <summary>
    ''' 添加记录
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub cmdAddRec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddRec.Click
        Dim NewRec As New CopyBookInfoRec
        With NewRec
            .CopyBookName = txtCopyBookName.Text
            If radData.Checked Then
                .CopyBookType = enmCopyBookType.Data
            Else
                .CopyBookType = enmCopyBookType.Procedure
            End If
            .IsDisposed = chkIsDisposed.Checked
            .FirstLayNo = NUDFirstLayNo.Value
            .LastLayNo = NUDLastLayNo.Value
        End With
        If mCopyBookInfoDic.ContainsKey(NewRec.CopyBookName) Then
            mCopyBookInfoDic.Remove(NewRec.CopyBookName)
        End If
        mCopyBookInfoDic.Add(NewRec.CopyBookName, NewRec)
        InitlizeForm()
    End Sub
    ''' <summary>
    ''' 删除选中记录
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub cmdDeleteRec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDeleteRec.Click
        If mCopyBookInfoDic.ContainsKey(txtCopyBookName.Text) Then
            mCopyBookInfoDic.Remove(txtCopyBookName.Text)
        End If
        InitlizeForm()
    End Sub
    ''' <summary>
    ''' 保存窗体
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        Dim mCopyBookInfoList As New clsCopyBookList
        Dim SaveCopyBookList As New List(Of CommonMethod.CopyBookInfoRec)
        For Each mCopyBookInfo As CommonMethod.CopyBookInfoRec In mCopyBookInfoDic.Values
            SaveCopyBookList.Add(mCopyBookInfo)
        Next
        mCopyBookInfoList.CopyBookInfo = SaveCopyBookList
        WriteObjectToXMLFile(mCopyBookInfoList, GetType(clsCopyBookList), MyConfig.CopyInfoFileName)
        Me.Close()
    End Sub
    ''' <summary>
    ''' 加载窗体
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub frmFileInfo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Dim mCopyBookInfoList As New clsCopyBookList
            mCopyBookInfoList = ReadObjectFromXMLFile(GetType(clsCopyBookList), MyConfig.CopyInfoFileName)
            mCopyBookInfoDic.Clear()
            For Each mCopyBookInfo As CommonMethod.CopyBookInfoRec In mCopyBookInfoList.CopyBookInfo
                mCopyBookInfoDic.Add(mCopyBookInfo.CopyBookName, mCopyBookInfo)
            Next
        Catch ex As Exception
            MsgBox("Can't Open File")
        End Try
        InitlizeForm()
    End Sub
    ''' <summary>
    ''' 重新初始化界面元素
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub InitlizeForm()
        Me.txtCopyBookName.Text = String.Empty
        Me.chkIsDisposed.Checked = False
        Me.radData.Checked = True
        lstCopyBookInfo.Items.Clear()
        Dim CopyBookNameList As New List(Of String)
        For Each strFileId As String In mCopyBookInfoDic.Keys
            CopyBookNameList.Add(strFileId)
        Next
        CopyBookNameList.Sort()
        For Each strFileId As String In CopyBookNameList
            With mCopyBookInfoDic(strFileId)
                Dim subItem As New ListViewItem
                subItem.Text = .CopyBookName
                subItem.SubItems.Add(.CopyBookType.ToString)
                subItem.SubItems.Add(.IsDisposed)
                subItem.SubItems.Add(.FirstLayNo)
                subItem.SubItems.Add(.LastLayNo)
                lstCopyBookInfo.Items.Add(subItem)
            End With
        Next
    End Sub
    ''' <summary>
    ''' 退出窗体
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
        Me.Close()
    End Sub
    ''' <summary>
    ''' 列表选中记录发生改变
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub lstFileInfo_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstCopyBookInfo.SelectedIndexChanged
        If lstCopyBookInfo.SelectedItems.Count > 0 Then
            Dim SelectFileName As String = lstCopyBookInfo.SelectedItems(0).Text
            With mCopyBookInfoDic(SelectFileName)
                Me.txtCopyBookName.Text = .CopyBookName
                Me.chkIsDisposed.Checked = .IsDisposed
                If .CopyBookType = enmCopyBookType.Data Then
                    radData.Checked = True
                Else
                    radProcedure.Checked = True
                End If
                NUDFirstLayNo.Value = .FirstLayNo
                NUDLastLayNo.Value = .LastLayNo
            End With
        End If
    End Sub
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub cmdBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSourceFilePath.Click
        Using mOpenFile As New OpenFileDialog
            mOpenFile.InitialDirectory = MyConfig.CopySourceFolder
            If mOpenFile.ShowDialog = Windows.Forms.DialogResult.OK Then
                txtCopyFilename.Text = mOpenFile.FileName
            End If
        End Using
    End Sub
    Private Sub cmdGenerateXML_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGenerateXML.Click
        clsCopyBook.GenerateXML(txtCopyFilename.Text)
        InitlizeForm()
    End Sub
    Private Sub cmdXMLFilePath_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdXMLFilePath.Click
        Using mOpenFile As New OpenFileDialog
            mOpenFile.InitialDirectory = MyConfig.CopyXMLFolder
            If mOpenFile.ShowDialog = Windows.Forms.DialogResult.OK Then
                txtXMLFilename.Text = mOpenFile.FileName
            End If
        End Using
    End Sub
End Class